<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { PlatFormNavMenuConfig as menuItems} from "@/views/PlatForm/const.js";

const router = useRouter()
const activeIndex = ref('1') // 默认激活的菜单项

const handleSelect = (index) => {
  const selectedItem = menuItems.find(item => item.index === index) ||
      [].concat(...menuItems.map(item => item.children || [])).find(child => child.index === index)
  if (selectedItem) {
    router.push({ name: selectedItem.routeName })
  }
}

</script>

<template>
  <el-menu :default-active="activeIndex" @select="handleSelect">
    <template v-for="item in menuItems" :key="item.index">
      <!-- 如果有子菜单 -->
      <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.index">
        <template #title>
          <i :class="item.icon"></i>
          <span>{{ item.title }}</span>
        </template>
        <el-menu-item v-for="child in item.children" :key="child.index" :index="child.index">
          {{ child.title }}
        </el-menu-item>
      </el-sub-menu>
      <!-- 如果没有子菜单 -->
      <el-menu-item v-else :index="item.index">
        <i :class="item.icon"></i>
        <span slot="title">{{ item.title }}</span>
      </el-menu-item>
    </template>
  </el-menu>
</template>

<style scoped lang="less">
.el-menu {
  border: 0 !important;
}
</style>
